package com.byzk.p2p.admin.system.controller;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.byzk.p2p.admin.base.controller.AdminBaseController;
import com.byzk.p2p.admin.base.util.DateUtils;
import com.byzk.p2p.admin.base.util.JsonMsg;
import com.byzk.p2p.admin.base.util.PageInfo;
import com.byzk.p2p.admin.base.util.SMSUtil;
import com.byzk.p2p.admin.system.dto.SMSRecord;
import com.byzk.p2p.admin.system.service.SMSService;
import com.byzk.p2p.admin.system.vo.SmsRecordVo;

/**
 * 短信测试
 * 
 * @author suker.zhou
 *
 */
@Controller
@RequestMapping("/admin/system/sms")
public class SmsController extends AdminBaseController {
	@Autowired
	private SMSService smsService;
	
	/**
	 * 短信发送
	 * @param model
	 * @param phone
	 * @param content
	 * @throws Exception
	 */
	@RequestMapping(value="/send",method = RequestMethod.POST)
	public void send(Model model, 
					@RequestParam("phone") String phone, 
					@RequestParam("content") String content) throws Exception {
		try {

			String[] temp=phone.split(",");
			for(int i=0;i<temp.length;i++){
				
				
				SMSRecord record=new SMSRecord();
				record.setCreateTime(new Date());
				record.setPhone(temp[i]);
				record.setContent(content);
				record.setSendIp(request.getRemoteAddr());
				record.setRemark("群发短信");
				record.setUserId(-1);
				
				smsService.addSmsRecord(record);
				
				//SMSUtil.sendSMS(temp[i], content);
			}
			super.printJSON(new JsonMsg(-1, "发送成功！"));
		} catch (Exception e) {
			super.printJSON(new JsonMsg(1, "格式错误"));
		}

		
	}

	/**
	 * 短信发送记录
	 * @param model
	 * @param index
	 * @param limit
	 * @param sendUser 短信发送人
	 * @param sendTimeFrom 发送时间开始
	 * @param sendTimeTo 发送时间结束
	 * @return
	 * @throws ParseException
	 */
	@RequestMapping(value="/list",method = RequestMethod.GET)
	public String listSmsRecord(Model model,
			@RequestParam(value = "index", required = false, defaultValue = "1") int index,
			@RequestParam(value = "limit", required = false, defaultValue = "10") int limit,
			@RequestParam(value = "sendUser", required = false) String sendUser,
			@RequestParam(value = "sendTimeFrom", required = false)String sendTimeFrom,
			@RequestParam(value = "sendTimeTo", required = false)String sendTimeTo) throws ParseException {
		Map<String, Object> queryMap = new HashMap<String, Object>();
		if (StringUtils.isNotBlank(sendUser)) {
			queryMap.put("sendUser", sendUser);
			model.addAttribute("sendUser", sendUser);
		}
		if (StringUtils.isNotBlank(sendTimeFrom)) {
			queryMap.put("sendTimeFrom",DateUtils.getRealDate(DateUtils.convert(sendTimeFrom, DateUtils.YEAR_MOUTH_DAY_FORMAT)));
			model.addAttribute("sendTimeFrom", sendTimeFrom);
		}
		if (StringUtils.isNotBlank(sendTimeTo)) {
			queryMap.put("sendTimeTo", DateUtils.getFullDate(DateUtils.convert(sendTimeTo, DateUtils.YEAR_MOUTH_DAY_FORMAT)));
			model.addAttribute("sendTimeTo", sendTimeTo);
		}
		PageInfo<SmsRecordVo> pageInfo = smsService.getSmsRecordList(queryMap, index, limit);
		model.addAttribute("pageInfo", pageInfo);
		return ftl(Thread.currentThread().getStackTrace());
	}

	/**
	 * 发送短信页
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/sendInfo")
	public String sendInfo(Model model) {
		return ftl(Thread.currentThread().getStackTrace());
	}
}
